﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.ServiceModel.Activities;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace MyActivityLibrary
{
    [Designer(typeof(CompositeDesigner))]
    public class CompositeActivity : NativeActivity
    {
        public Collection<Activity> Branches { get; set; }
        public Collection<Variable> Variables { get; set; }
        public CompositeActivity()
        {
            Branches = new Collection<Activity>();
            Variables = new Collection<Variable>();
        }

        int activityCounter = 0;
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            metadata.SetChildrenCollection(Branches);
            metadata.SetVariablesCollection(Variables);
        }
        protected override void Execute(NativeActivityContext context)
        {
            ScheduleActivities(context);
        }
        void ScheduleActivities(NativeActivityContext context)
        {
            if (activityCounter < Branches.Count)
                context.ScheduleActivity(this.Branches[activityCounter++], OnActivityCompleted);
        }
        void OnActivityCompleted(NativeActivityContext context, ActivityInstance completedInstance)
        {
            ScheduleActivities(context);
        }
    }
}